<?php

/**
 * This is the model class for table "zy_campaign".
 *
 * The followings are the available columns in table 'zy_campaign':
 * @property integer $zy_campaign_id
 * @property string $zy_campaign_script
 * @property string $sales_people
 * @property integer $zy_potentiel_customers_list_id
 * @property string $zy_campaign_status
 */
class Campaign extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Campaign the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_campaign';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zy_campaign_script', 'required'),
			array('zy_campaign_status', 'numerical', 'integerOnly'=>true),
			array('campaign_name,zy_campaign_script, sales_people, zy_campaign_status,zy_potentiel_customers_list_id,opentime,closetime', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zy_campaign_id,campaign_name, zy_campaign_script, sales_people, zy_potentiel_customers_list_id, zy_campaign_status,opentime,closetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zy_campaign_id' =>Yii::t('base','Campaign'),
			'campaign_name'=>Yii::t('base','Campaign Name'),
			'zy_campaign_script' => Yii::t('base','Campaign Script'),
			'sales_people' => Yii::t('base','Sales People'),
			'zy_potentiel_customers_list_id' => Yii::t('base','Potentiel Customers List'),
			'zy_campaign_status' => Yii::t('base','Campaign Status'),
			'opentime'=>Yii::t('base','Opentime'),
			'closetime'=>Yii::t('base','Closetime'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zy_campaign_id',$this->zy_campaign_id);
		$criteria->compare('campaign_name',$this->campaign_name,true);
		$criteria->compare('zy_campaign_script',$this->zy_campaign_script,true);
		$criteria->compare('sales_people',$this->sales_people,true);
		$criteria->compare('zy_potentiel_customers_list_id',$this->zy_potentiel_customers_list_id);
		$criteria->compare('zy_campaign_status',$this->zy_campaign_status,true);
		$criteria->compare('opentime',$this->opentime,true);
		$criteria->compare('closetime',$this->closetime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//通过电话销售人员的id得到她的名字
	public function getSalesPeopleName($id)
	{
		$usernamelist=array();
		$arr = str_replace("|", ",", $id);	
		$sql="select * from zy_user where user_id in ($arr)";
		$result=Yii::app()->db->createCommand($sql)->queryAll();
		for($i=0;$i<count($result);$i++)
		{
		//$salespeoplename=implode('|',$result[$i]['user_name']);
		$usernamelist[$i]= $result[$i]['user_name'];
		}
		$salesPeopleName=implode('|',$usernamelist);
		return $salesPeopleName;
	}	
	
	//通过销售活动状态得到名字
	public function getcampaignstatusName($status)
	{
	if($status==0)
	{
	$campaignstatusName="开启";
	}elseif ($status==1)
	{
		$campaignstatusName="关闭";
	}
	return $campaignstatusName;
	}
	//通过活动id得到活动的名字
	public function getCampaignName($id)
	{
		$campaign = Campaign::model()->findByPk($id);
		return $campaign->campaign_name;
	}
	//通过活动id得到活动的script
	public function getCampaignScript($id)
	{
		$campaign = Campaign::model()->findByPk($id);
		return $campaign->zy_campaign_script;
	}
}